package com.uetoken.cn.adapter;

import android.content.Context;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.uetoken.cn.R;
import com.uetoken.cn.adapter.model.ChildPurseAssetModel;
import com.uetoken.cn.bean.SubPursesBean;

import java.util.List;

public class ChildPurseAassetAdapter extends GroupedRecyclerViewAdapter {

    private List<ChildPurseAssetModel> mGroups;
    private List<SubPursesBean.DataBean> mChildren;
    private Context mContext;
    private int childrenSize;

    public ChildPurseAassetAdapter(Context context) {
        super(context);
        this.mContext = context;
    }

    public ChildPurseAassetAdapter(Context context, List<ChildPurseAssetModel> groups) {
        super(context);
        this.mGroups = groups;
        this.mContext = context;
    }

    public void setData(List<ChildPurseAssetModel> groups) {
        this.mGroups = groups;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return mGroups == null ? 0 : mGroups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        //如果当前组收起，就直接返回0，否则才返回子项数。这是实现列表展开和收起的关键。
        if (!isExpand(groupPosition)) {
            return 0;
        }
        mChildren = mGroups.get(groupPosition).getDataBeanList();
        childrenSize = mChildren == null ? 0 : mChildren.size();
        return childrenSize;
    }

    @Override
    public boolean hasHeader(int groupPosition) {
        return true;
    }

    @Override
    public boolean hasFooter(int groupPosition) {
        return false;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        return R.layout.item_child_purse_assets;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return 0;
    }

    @Override
    public int getChildLayout(int viewType) {
        return R.layout.item_child_purse_assets;
    }

    @Override
    public void onBindHeaderViewHolder(BaseViewHolder holder, int groupPosition) {
        String mAccountName = mContext.getResources().getString(R.string.str_not_available);
        if (mGroups != null) {
            if (mGroups.size() > 0) {
                ChildPurseAssetModel entity = mGroups.get(groupPosition);
                SubPursesBean.DataBean mDataBean = entity.getDataBean();
                if (mDataBean != null) {
                    mAccountName = mDataBean.getPursename() + "(" + mDataBean.getPursecode() + ")";
                    holder.setVisible(R.id.childPurseIv, View.VISIBLE);
                    if (entity.isExpand()) {
                        holder.setImageResource(R.id.childPurseIv, R.mipmap.icon_arrow_up);
                    } else {
                        holder.setImageResource(R.id.childPurseIv, R.mipmap.icon_arrow_down);
                    }
                }


            }
        }
        mChildren = mGroups.get(groupPosition).getDataBeanList();
        childrenSize = mChildren == null ? 0 : mChildren.size();
        if (childrenSize <= 0) {
            holder.setVisible(R.id.childPurseIv, View.INVISIBLE);
        }
        holder.setText(R.id.childPurseNameTv, mAccountName);
    }

    @Override
    public void onBindFooterViewHolder(BaseViewHolder holder, int groupPosition) {
    }

    @Override
    public void onBindChildViewHolder(BaseViewHolder holder, int groupPosition, int childPosition) {
        SubPursesBean.DataBean entity = mChildren.get(childPosition);
        String mAccountName = entity.getPursename() + "(" + entity.getPursecode() + ")";
        holder.setVisible(R.id.childPurseToTv, View.GONE);
        holder.setText(R.id.childPurseNameTv, mAccountName);
        holder.setVisible(R.id.childPurseIv, View.INVISIBLE);
    }

    /**
     * 判断当前组是否展开
     *
     * @param groupPosition
     * @return
     */
    public boolean isExpand(int groupPosition) {
        ChildPurseAssetModel entity = mGroups.get(groupPosition);
        return entity.isExpand();
    }

    /**
     * 展开一个组
     *
     * @param groupPosition
     */
    public void expandGroup(int groupPosition) {
        expandGroup(groupPosition, false);
    }

    /**
     * 展开一个组
     *
     * @param groupPosition
     * @param animate
     */
    public void expandGroup(int groupPosition, boolean animate) {
        ChildPurseAssetModel entity = mGroups.get(groupPosition);
        entity.setExpand(true);
        if (animate) {
            notifyChildrenInserted(groupPosition);
        } else {
            notifyDataChanged();
        }
    }

    /**
     * 收起一个组
     *
     * @param groupPosition
     */
    public void collapseGroup(int groupPosition) {
        collapseGroup(groupPosition, false);
    }

    /**
     * 收起一个组
     *
     * @param groupPosition
     * @param animate
     */
    public void collapseGroup(int groupPosition, boolean animate) {
        ChildPurseAssetModel entity = mGroups.get(groupPosition);
        entity.setExpand(false);
        if (animate) {
            notifyChildrenRemoved(groupPosition);
        } else {
            notifyDataChanged();
        }
    }
}

